home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2873 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.0 KB

  1. Path: prairienet.org!sjmccaug
  2. From: sjmccaug@prairienet.org (Scott J. McCaughrin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q]Assigning function pointer in C/C++.
  5. Date: 20 Jan 1996 04:10:19 GMT
  6. Organization: University of Illinois at Urbana
  7. Message-ID: <4dppvb$6b3@vixen.cso.uiuc.edu>
  8. References: <4doc42$gsb@bmdhh222.bnr.ca> <DL3JJu.5nB.0.queen@torfree.net>
  9. Reply-To: sjmccaug@prairienet.org (Scott J. McCaughrin)
  10. NNTP-Posting-Host: firefly.prairienet.org
  11.  
  12.  
  13. In a previous article, ray.fallon@nt.com (Ray Fallon) says:
  14.  
  15. >bh332@freenet.toronto.on.ca (Karim Ladha) wrote:
  16. >>
  17. >>
  18. >>How is it possible to assign a declared variable in C++ a pointer to
  19. >>some function member? If you know of a solution, post. Greatly appreciated.
  20. >>
  21. >>In C for example;
  22. >>
  23. >>...
  24. >>void( far *MyFunc )();
  25. >>...
  26. >>...
  27. >>
  28. >>Thank you,
  29. >>  Karim A.Ladha - BH332@TorFree.Net
  30. >>
  31. >>PS: Using BC++ v4.0 for Windows(tm) - DOS Application.
  32. >>
  33. >>-- 
  34. >>
  35. >
  36. >Hi there Karim,
  37. >
  38. >I response to your question, assigment of pointers to member functions is
  39. >not possible for the following reason : 
  40. >
  41. >In 'C' you can only have one instance of a function at any one time. In 
  42. >C++ you can have multiple instances of a class (i.e. objects) and the
  43. >pointer to the function would not be able to know which object you are
  44. >referring to. In any case, it is possible to use other methods, and 
  45. >theoritically they are more sound methods. Use the principle of 
  46. >polymorphism. There is no scenario where you think that pointers to 
  47. >functions can be used and polymorphism cannot. Consider the following 
  48. >scenario :
  49.  As usual, the C++ crowd has to bury us with voluminous programming examples
  50.  which only serve to obfuscate the issue at hand. Why is simplicity so
  51.  abhorrent to C++'ers? The author didn't ask about 'member' functions, so
  52.  even a 'friend' function would have sufficed. The simple answer is that
  53.  functions -- like arrays -- represent their addresses when appearing as
  54.  names by themselves. Thus if 'SomeFunc' were a function of 0-arity returning
  55.  void, just say:  MyFunc = SomeFunc;
  56.  
  57.  -- Scott McC.
  58.